/**
 * Copyright (C), 2018-2018, 孙春磊有限公司
 * FileName: UserController
 * Author:   Administrator
 * Date:     2018/11/1 14:25
 * Description: 控制层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.yh.controller;

import com.github.pagehelper.Page;
import com.yh.entity.Result;
import com.yh.entity.User;
import com.yh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈控制层〉
 *
 * @author Administrator
 * @create 2018/11/1
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/toList")
    public ModelAndView toList() {
        return new ModelAndView("list");
    }

    @RequestMapping("/getPage")
    public Map queryUserByPage(Integer pageSize, Integer pageNum, User user){
        Map map = new HashMap();
        map.put("pageSize",pageSize );
        map.put("pageNum",pageNum);
        map.put("user",user);
        Page page = userService.queryLikeUsers(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }
    @RequestMapping("/addUser")
    public Result addUser(User user){
        userService.addUser(user);
        return new Result();
    }

    @RequestMapping("/delUser")
    public Result delUser(User user){
        userService.delUser(user.getId());
        return new Result();
    }
    @RequestMapping("/editUser")
    public ModelAndView getUser(User user) {
        User u = userService.getUserById(user.getId());
        Map<String, Object> map = new HashMap();
        map.put("data", u);
        return new ModelAndView("edit", map);
    }

    @RequestMapping("/updateUser")
    public Result updateUser(User user) {
        userService.updateUser(user);
        return new Result();
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
